package com.zhuoyue.common.utils;

import java.io.UnsupportedEncodingException;
/**
 * @ClassName: GB2312
 * @Description:
 * @author wjming
 * @Date 2021-07-14 20:21
 */
public class Gb2312 {
    public static String chineseToGb(String gb) throws UnsupportedEncodingException {
        // 先把字符串按gb2312转成byte数组
        byte[] bytes = gb.getBytes("gb2312");
        StringBuilder gbString = new StringBuilder();
        // 循环数组
        for (byte b : bytes)
        {
            // 再用Integer中的方法，把每个byte转换成16进制输出
            String temp = Integer.toHexString(b);
            // 截取
            temp = temp.substring(6, 8);
            gbString.append(temp);
        }
        return gbString.toString();
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(chineseToGb("测试"));
        System.out.println(chineseToGb("测试").length()+"");
    }
}
